package com.annotation.demo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class AnnotationParser {

	
	public static void parseAnnotationField(AnnotationUsageObject object, Class<?> clazz) {

		Field[] fields = clazz.getDeclaredFields();
		for (Field f : fields) {

			if (f.isAnnotationPresent(AnnotationMyFieldColor.class)) {
				AnnotationMyFieldColor c = (AnnotationMyFieldColor) f.getAnnotation(AnnotationMyFieldColor.class);
				String cm = "Field: " + clazz.getName() + "." + f.getName();
				String ca = AnnotationMyFieldColor.class.getName() + " -- " + c.color();
				object.annotationNote += cm + "(), with color annotation @" + ca + "\n";

			} else if (f.isAnnotationPresent(AnnotationMyFieldLocation.class)) {
				AnnotationMyFieldLocation l = (AnnotationMyFieldLocation) f.getAnnotation(AnnotationMyFieldLocation.class);
				String cm = "Field: " + clazz.getName() + "." + f.getName();
				String ca = AnnotationMyFieldLocation.class.getName() + " -- " + l.location();
				object.annotationNote += cm + "(), with location annotation @" + ca + "\n";
			}
		}

	}

	public static void parseAnnotationMethod(AnnotationUsageObject object, Class<?> clazz) {

		Method[] methods = clazz.getDeclaredMethods();
		for (Method m : methods) {
			if (m.isAnnotationPresent(AnnotationMyMethodCook.class)) {
				AnnotationMyMethodCook cook = (AnnotationMyMethodCook) m.getAnnotation(AnnotationMyMethodCook.class);
				String cm = "Method: " + clazz.getName() + "." + m.getName();
				String ca = AnnotationMyMethodCook.class.getName() + " -- " + cook.description() + cook.name();
				object.annotationNote += cm + "(), with cook annotation @" + ca + "\n";
			} else if (m.isAnnotationPresent(AnnotationMyMethodEat.class)) {
				AnnotationMyMethodEat eat = (AnnotationMyMethodEat) m.getAnnotation(AnnotationMyMethodEat.class);
				String cm = "Method: " + clazz.getName() + "." + m.getName();
				String ca = AnnotationMyMethodEat.class.getName() + " -- " + eat.description() + eat.name();
				object.annotationNote += cm + "(), with eat annotation @" + ca + "\n";
			}
		}

	}

}
